/*
 * Copyright(c) 2018 优居科技 All rights reserved.
 * distributed with this file and available online at
 * http://www.yjyz.com/
 */
package com.zb.api.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.zb.api.business.dto.*;
import com.zb.api.business.entity.PosMerchantEntity;
import com.zb.api.business.entity.PosPlanEntity;
import com.zb.api.business.entity.PosPlanUpEntity;
import com.zb.api.business.entity.UserEntity;
import com.zb.api.business.mapper.PosMerchantMapper;
import com.zb.api.business.mapper.PosPlanMapper;
import com.zb.api.business.mapper.PosUserCollectMapper;
import com.zb.api.business.mapper.PosWaresMapper;
import com.zb.api.business.service.IPosPlanService;
import com.zb.api.common.econts.*;
import com.zb.api.common.models.Page;
import com.zb.api.utils.UserThreadLocal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
* 方案表
 *
 * @author cwp
 * @version v1.0
 * @CreationTime: - 2022-02-08 14:19:31
 * @Description:
 */
@Service
public class PosPlanServiceImpl implements IPosPlanService {

    @Autowired
    private PosPlanMapper posPlanMapper;

    @Autowired
    private PosMerchantMapper posMerchantMapper;

    @Autowired
    private PosWaresMapper posWaresMapper;

    @Autowired
    private PosUserCollectMapper posUserCollectMapper;




    @Override
    public Page<PosPlanDto> getPosPlanList(int pageNo, int pageSize, PosPlanDto posPlanDto){
        /*PageHelper.startPage(pageNo, pageSize);
        PosPlanEntity posPlanEntity = BeansUtils.copyProperties(posPlanDto, PosPlanEntity.class);
        return BeansUtils.toPage(posPlanMapper.getList(posPlanEntity), PosPlanDto.class);*/
        return null;
    }

    @Override
    public PosPlanDto get(String id) {
        PosPlanDto posPlanDto = posPlanMapper.get(id);
        if (posPlanDto.getThemeStyle()!=null){
            posPlanDto.setThemeStyleName(DecorationStyleEnum.value(posPlanDto.getThemeStyle()));
        }
        return posPlanDto;
    }

    @Override
    public int update(PosPlanDto posPlan){
        PosPlanEntity entity = new PosPlanEntity();
        BeanUtils.copyProperties(posPlan, entity);
        return posPlanMapper.update(entity);
    }

    @Override
    public int insert(PosPlanDto posPlan){
        PosPlanEntity entity = new PosPlanEntity();
        BeanUtils.copyProperties(posPlan, entity);
        return posPlanMapper.insert(entity);
    }

    @Override
    public int delete(PosPlanDto posPlan){
        PosPlanEntity entity = new PosPlanEntity();
        BeanUtils.copyProperties(posPlan, entity);
        return posPlanMapper.delete(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertPlan(PosPlanDto posPlanDto) throws IllegalAccessException {
        System.out.println(JSON.toJSONString(posPlanDto));
        PosMerchantEntity posMerchantEntity =posMerchantMapper.get(posPlanDto.getMerchantId().toString());


        PosPlanEntity entity = new PosPlanEntity();
        BeanUtils.copyProperties(posPlanDto, entity);
        //插入一条方案信息
        entity.setCreateTm(new Date().getTime());
        if (posMerchantEntity!=null){
            entity.setCityCode(posMerchantEntity.getCityCode());
            entity.setCityName(posMerchantEntity.getCityName());
        }
        posPlanMapper.insert(entity);
        //判断是否存在包含多个房间并统计 面积 金额
        if (!CollectionUtils.isEmpty(posPlanDto.getUpDtoList())){
            BigDecimal price = BigDecimal.ZERO;
            for (PosPlanUpDto posPlanUpDto : posPlanDto.getUpDtoList()) {
                PosPlanUpEntity upEntity = new PosPlanUpEntity();
                BeanUtils.copyProperties(posPlanUpDto, upEntity);
                upEntity.setPlanId(entity.getId());
                upEntity.setCreateTm(new Date().getTime());
                upEntity.setMerchantId(entity.getMerchantId());
                upEntity.setMerchantName(entity.getMerchantName());
                posPlanMapper.insertUp(upEntity);
                price = price.add(posPlanUpDto.getPrice());
            }
            if (price.compareTo(posPlanDto.getPlanPrice()) == 1){
                throw new IllegalAccessException("房屋价格总和不能大于方案价格");
            }
        }
        return 1;
    }

    @Override
    public Integer updatePlan(PosPlanDto posPlanDto) throws IllegalAccessException {
        PosPlanEntity entity = new PosPlanEntity();
        BeanUtils.copyProperties(posPlanDto, entity);
        //插入一条方案信息
        posPlanMapper.update(entity);
        //删除原来相关的房间信息
        PosPlanUpEntity planUpEntity = new PosPlanUpEntity();
        planUpEntity.setPlanId(posPlanDto.getId());
        posPlanMapper.deleteUp(planUpEntity);
        //判断是否存在包含多个房间
        if (!CollectionUtils.isEmpty(posPlanDto.getUpDtoList())){
            BigDecimal price = BigDecimal.ZERO;
            for (PosPlanUpDto posPlanUpDto : posPlanDto.getUpDtoList()) {
                PosPlanUpEntity upEntity = new PosPlanUpEntity();
                BeanUtils.copyProperties(posPlanUpDto, upEntity);
                upEntity.setPlanId(entity.getId());
                posPlanMapper.insertUp(upEntity);
                price = price.add(posPlanUpDto.getPrice());
            }
            if (price.compareTo(posPlanDto.getPlanPrice()) == 1){
                throw new IllegalAccessException("房屋价格总和不能大于方案价格");
            }
        }
        return 1;
    }

    @Override
    public Page<PosPlanDto> getPlanList(PosPlanDto posPlanDto) {
        Page<PosPlanDto> page = new Page<>();
        List<PosPlanDto> list = posPlanMapper.getList(posPlanDto);
        if (!CollectionUtils.isEmpty(list)){
            for (PosPlanDto planDto : list) {
                if (planDto.getThemeStyle()!=null){
                    planDto.setThemeStyleName(DecorationStyleEnum.value(planDto.getThemeStyle()));
                }
            }
        }
        page.setList(list);
        page.setPageNo(posPlanDto.getPageNo());
        page.setRowCntPerPage(posPlanDto.getPageSize());
        return page;

    }

    @Override
    public PosPlanDto getPlanInfo(PosPlanDto planDto) {
        PosPlanDto posPlanDto = posPlanMapper.get(planDto.getId()+"");
        if (posPlanDto!=null){
            PosPlanUpDto posPlanUpDto = new PosPlanUpDto();
            posPlanUpDto.setPlanId(planDto.getId());
            List<PosPlanUpDto> planUpDtos = posPlanMapper.getUpList(posPlanUpDto);
            if (!CollectionUtils.isEmpty(planUpDtos)){
                for (PosPlanUpDto planUpDto : planUpDtos) {
                    if (!StringUtils.isEmpty(planUpDto.getWaresCodes())){
                        List<String> idList = Arrays.asList(planUpDto.getWaresCodes().split(","));
                        PosWaresDto posWares = new PosWaresDto();
                        posWares.setIdStr(idList);
                        List<PosWaresDto> waresDtos = posWaresMapper.getList(posWares);
                        if (!CollectionUtils.isEmpty(waresDtos)){
                            for (PosWaresDto waresDto : waresDtos) {
                                if (waresDto.getType()!=null){
                                    waresDto.setTypeName(WaresTypeEnum.valueOf(waresDto.getType()).getDesc());
                                }
                            }
                            planUpDto.setPosWaresDtos(waresDtos);
                        }
                    }
                    if (planUpDto.getType()!=null){
                        planUpDto.setTypeName(HallRoomEnum.valueOf(planUpDto.getType()).getDesc());
                    }
                }
            }
            posPlanDto.setUpDtoList(planUpDtos);
            if (posPlanDto.getRoom()!=null){
                posPlanDto.setRoomName(RoomEnum.valueOf(posPlanDto.getRoom()).getDesc());
            }
            if (posPlanDto.getType()!=null){
                posPlanDto.setTypeName(LabelEnum.valueOf(posPlanDto.getType()).getDesc());
            }
            if (posPlanDto.getThemeStyle()!=null){
                posPlanDto.setThemeStyleName(DecorationStyleEnum.value(posPlanDto.getThemeStyle()));
            }
            if (posPlanDto.getDesignerId()!=null){
                UserDesignerDto userDesignerDto = posMerchantMapper.getDesigner(posPlanDto.getDesignerId());
                posPlanDto.setDesignerName(userDesignerDto.getName());
                posPlanDto.setDesignerPhone(userDesignerDto.getPhone());
            }
            UserEntity user =  UserThreadLocal.get();
            PosUserCollectDto posUserCollectDto = new PosUserCollectDto();
            posUserCollectDto.setUserId(user.getId());
            posUserCollectDto.setPlanId(posPlanDto.getId());
            List<PosUserCollectDto> collectDtos = posUserCollectMapper.getList(posUserCollectDto);
            if (!CollectionUtils.isEmpty(collectDtos)){
                posPlanDto.setCollectId(collectDtos.get(0).getId());
            }
        }
        return posPlanDto;
    }

    @Override
    public Integer cancelPlan(PosPlanDto posPlanDto) {
        PosPlanEntity entity = new PosPlanEntity();
        BeanUtils.copyProperties(posPlanDto, entity);
        //插入一条方案信息
        posPlanMapper.update(entity);
        return entity.getId();
    }

    @Override
    public Page<PosPlanDto> getRecommendList(PosPlanDto posPlanDto) {
        Page<PosPlanDto> page = new Page<>();
        page.setPageNo(posPlanDto.getPageNo());
        page.setRowCntPerPage(posPlanDto.getPageSize());
        UserEntity user =  UserThreadLocal.get();
        if (user.getRemarks()!=null){
            List<String> styles = Arrays.asList(user.getRemarks().split(","));
            posPlanDto.setThemeStyles(styles);
        }
        List<PosPlanDto> posPlanDtos = posPlanMapper.getList(posPlanDto);
        if (!CollectionUtils.isEmpty(posPlanDtos)){
            for (PosPlanDto planDto : posPlanDtos) {
                if (planDto.getThemeStyle()!=null){
                    posPlanDto.setThemeStyleName(DecorationStyleEnum.value(posPlanDto.getThemeStyle()));
                }
            }
        }
        page.setList(posPlanDtos);
        return page;
    }

    @Override
    public List<PosPlanDto> getHotList() {
        PosUserCollectDto posUserCollect = new PosUserCollectDto();
        List<PosPlanDto> planDtos = posUserCollectMapper.getCountCollect(posUserCollect);

        PosPlanDto posPlanDto = new PosPlanDto();
        if (!CollectionUtils.isEmpty(planDtos)){
            posPlanDto.setIds(planDtos.stream().map(m -> m.getId()).collect(Collectors.toList()));
        }
        return posPlanMapper.getList(posPlanDto);
    }

}
